package com.personal.website.lottery.web.controller;

import com.personal.website.lottery.web.service.ArticleService;
import com.personal.website.lottery.web.service.DoubleColorBallService;
import com.personal.website.lottery.web.service.NoticeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by Gavin on 2016/2/5.
 */
@Controller
@RequestMapping("/")
public class IndexController {

    @Resource
    private ArticleService articleService;
    @Resource
    private DoubleColorBallService doubleColorBallService;
    @Resource
    private NoticeService noticeService;

    private final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @RequestMapping({"", "index"})
    public String index() {
        return "index";
    }

    @RequestMapping({"indexContent"})
    public String indexContent(HttpServletRequest request, Model model) {
        try {
            String maxArticleSize = request.getParameter("maxArticleSize");
            int maxSize = 30;
            if(maxArticleSize != null && maxArticleSize.trim() != ""){
                maxSize = Integer.parseInt(maxArticleSize);
            }
            if(maxSize <= 0 || maxSize > 100) {
                maxSize = 30;
            }

            model.addAttribute("articles", this.articleService.getArticles(maxSize));
            model.addAttribute("latestBall", this.doubleColorBallService.getLatestBall());
            model.addAttribute("notices", this.noticeService.getNoticeList(3));
        } catch (Exception e) {
            logger.error("indexContent页面加载数据失败！", e);
        }
        return "indexContent";
    }

}
